package com.tueye.framework.core.template.support;

import java.io.IOException;

import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ResourceLoaderAware;

import freemarker.template.Configuration;
import freemarker.template.Template;

/**
 * 类说明：模板资源加载
 * 
 * @author hanxiaowei
 * 
 */
public class FreeMarkerConfigurationFactory extends FreeMarkerConfiguration
		implements FactoryBean, InitializingBean, ResourceLoaderAware {

	private Configuration configuration;

	@Override
	public Object getObject() throws Exception {

		return this.configuration;
	}

	@Override
	public Class getObjectType() {

		return Configuration.class;
	}

	public Template getTemplate(String name) {
		try {
			return this.configuration.getTemplate(name);
		} catch (IOException e) {
			logger.error("查找模板资源文件出错,模板" + name + "没有找到", e);
		}
		return null;
	}

	@Override
	public boolean isSingleton() {

		return true;
	}

	@Override
	public void afterPropertiesSet() throws Exception {
		if (this.configuration == null) {
			this.configuration = createConfiguration();
		}
	}

}
